package com.learn.test01;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

class Team {
    private String name;          // 球队名称
    private int matchesPlayed;    // 已比赛场数
    private int wins;             // 赢的场数
    private int draws;            // 平的场数
    private int losses;           // 负的场数
    private int goalsFor;         // 进球数
    private int goalsAgainst;     // 失球数
    private int points;           // 积分

    public Team(String name) {
        this.name = name;
        this.matchesPlayed = 0;
        this.wins = 0;
        this.draws = 0;
        this.losses = 0;
        this.goalsFor = 0;
        this.goalsAgainst = 0;
        this.points = 0;
    }

    // 更新球队比赛结果
    public void updateResult(int scored, int conceded) {
        matchesPlayed++;
        goalsFor += scored;
        goalsAgainst += conceded;

        if (scored > conceded) {        // 赢得比赛
            wins++;
            points += 3;
        } else if (scored == conceded) { // 平局
            draws++;
            points += 1;
        } else {                        // 输掉比赛
            losses++;
        }
    }

    // 计算净胜球
    public int getGoalDifference() {
        return goalsFor - goalsAgainst;
    }

    // getters
    public String getName() { return name; }
    public int getPoints() { return points; }
    public int getGoalsFor() { return goalsFor; }
    public int getMatchesPlayed() { return matchesPlayed; }

    // 返回球队的当前统计信息
    public String toString() {
        return String.format("%-15s %2d %2d %2d %2d %3d %3d %3d",
                name, matchesPlayed, wins, draws, losses, goalsFor, goalsAgainst, points);
    }
}

class League {
    private ArrayList<Team> teams;

    public League() {
        teams = new ArrayList<>();
    }

    // 添加一个球队
    public void addTeam(Team team) {
        teams.add(team);
    }

    // 查找球队
    public Team findTeam(String name) {
        for (Team team : teams) {
            if (team.getName().equalsIgnoreCase(name)) {
                return team;
            }
        }
        return null;
    }
    public void recordMatch(String homeTeamName, String awayTeamName, int homeScore, int awayScore) {
        Team homeTeam = findTeam(homeTeamName);
        Team awayTeam = findTeam(awayTeamName);

        if (homeTeam != null && awayTeam != null) {
            homeTeam.updateResult(homeScore, awayScore);
            awayTeam.updateResult(awayScore, homeScore);
        }
    }

    public void sortTeams() {
        Collections.sort(teams, new Comparator<Team>() {
            public int compare(Team t1, Team t2) {
                if (t1.getPoints() != t2.getPoints()) {
                    return t2.getPoints() - t1.getPoints(); // 积分高的在前
                }
                if (t1.getGoalDifference() != t2.getGoalDifference()) {
                    return t2.getGoalDifference() - t1.getGoalDifference(); // 净胜球高的在前
                }
                return t2.getGoalsFor() - t1.getGoalsFor(); // 进球多的在前
            }
        });
    }

    // 显示积分表
    public void displayLeagueTable() {
        System.out.printf("%-15s %2s %2s %2s %2s %3s %3s %3s\n",
                "队名", "场次", "胜", "平", "负", "进球", "失球", "积分");
        for (Team team : teams) {
            System.out.println(team);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        League league = new League();
        league.addTeam(new Team("TeamA"));
        league.addTeam(new Team("TeamB"));
        league.addTeam(new Team("TeamC"));
        league.addTeam(new Team("TeamD"));

        Scanner scanner = new Scanner(System.in);

        // 循环输入比赛结果
        System.out.println("请输入比赛结果（格式：主队 客队 主队得分 客队得分）：");
        while (true) {
            System.out.print("主队：");
            String homeTeam = scanner.next();
            System.out.print("客队：");
            String awayTeam = scanner.next();

            if (homeTeam.equalsIgnoreCase(awayTeam)) {
                System.out.println("错误：主队和客队不能相同，请重新输入。");
                continue;
            }

            System.out.print("主队得分：");
            int homeScore = readValidScore(scanner);
            System.out.print("客队得分：");
            int awayScore = readValidScore(scanner);

            if (homeTeam.equalsIgnoreCase("exit")) break; // 输入 "exit" 退出

            if (league.findTeam(homeTeam) == null || league.findTeam(awayTeam) == null) {
                System.out.println("错误：输入的球队名称不存在，请重新输入。");
                continue;
            }

            if (homeScore < 0 || awayScore < 0) {
                System.out.println("错误：得分不能为负数，请重新输入。");
                continue;
            }

            league.recordMatch(homeTeam, awayTeam, homeScore, awayScore);

            league.sortTeams();      // 更新排序
            league.displayLeagueTable();  // 显示最新积分表
        }
        scanner.close();
    }

    private static int readValidScore(Scanner scanner) {
        while (true) {
            if (!scanner.hasNextInt()) {
                System.out.println("错误：得分必须是整数，请重新输入。");
                scanner.next(); // 清除非法输入
            } else {
                return scanner.nextInt();
            }
        }
    }
}